package com.sumer.psychology.space.infrastructure.persistence.mongo;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sumer.psychology.common.persistence.mongodb.AbstractMongoRepository;

/**
 * 个人信息
 * <p/>
 * Created by twer on 15/3/29.
 */
public class MongoProfileRepository extends AbstractMongoRepository {
    private static final String DB_NAME = "sumer_profile";

    @Override
    protected String dbName() {
        return DB_NAME;
    }

    /**
     * 增加信息
     *
     * @param userId  user id
     * @param photoId photo id
     */
    public void edit(String userId, String photoId) {
        db().getCollection(dbName()).update(
                new BasicDBObject("_id", userId),
                new BasicDBObject("$set", new BasicDBObject("photo_id", photoId)),
                true,
                false);
    }

    /**
     * 查找照片
     *
     * @param userId user id
     * @return photo id
     */
    public String findPhoto(String userId) {
        DBObject profile = findOne(userId);
        if (profile == null) {
            return null;
        }
        return (String) profile.get("photo_id");
    }
}
